#!/bin/bash
#
#******************************************************************************
# pack (midicvt)
#------------------------------------------------------------------------------
##
# \file       	pack
# \library    	midicvt
# \author     	Chris Ahlstrom
# \date       	2014-04-08
# \update     	2014-06-03
# \version    	$Revision$
# \license    	$WAONC_SUITE_GPL_LICENSE$
#
#  Packs up the current project directory, ignoring some of the derived
#  junk and the version-control infrastructure.
#
#  Run "./pack --help" for more information.
#
#-----------------------------------------------------------------------------

CURRENTDATE=`date +%Y-%m-%d`
CURRENTDIR=`pwd`
WORKINGDIR=${CURRENTDIR##/*/}       # strip all but last part of path

# TAGSTRING="git"

TAGSTRING="pack"
DODRYRUN="no"
DOCLEAN="no"

BRANCH=`git symbolic-ref -q HEAD 2> /dev/null`

if [ $? == 0 ] ; then

   ISGITBRANCH="yes"
   GITBRANCH=${BRANCH##*/}

else

   ISGITBRANCH="no"
   GITBRANCH=""

fi

if [ "$1" == "--help" ] || [ "$1" == "help" ] ; then

   echo
   echo "pack 0.2 edited on 2014-06-03"
   echo
   echo "Usage:  ./pack [--dry-run] [--clean] [tag]"
   echo
   echo "where tag is alternate information you want to include in the"
   echo "name of the tarball; it replaces the current date."
   echo
   echo "This script packs the contents of the current directory into a"
   echo "tarball that has the name of the directory and other information"
   echo "as part of the filename."
   echo
   echo "This script packs the entire current directory ('$WORKINGDIR') into"
   echo "a file named like the following (using no tag as an example):"
   echo
   echo "   $WORKINGDIR-master-$TAGSTRING-my-code.tar.bz2"
   echo "   $WORKINGDIR-$TAGSTRING-my-code.tar.bz2"
   echo
   echo "Excluded from the tarball are derived products such as the 'Debug',"
   echo "'Release', 'html', and 'latex' directories, as well as object"
   echo "modules and the database/symbol files generated by Visual Studio"
   echo "or Borland.  Version-control-system directories are also ignored."
   echo
   echo "This script also detects the presence of a git branch, and incorporates"
   echo "the branch name into the name of the tarball."
   echo

else

   while [ "$1" != "" ] ; do

      case "$1" in
      
#        Although this option is no longer needed, keep it around as an
#        undocumented feature just in case the user wants to override the
#        git discovery.

        --branch | --git)
            ISGITBRANCH="yes"
            shift
            GITBRANCH="$1"
            ;;

        --dry-run)
            DODRYRUN="yes"
            ;;

        --clean)
            DOCLEAN="yes"
            ;;

        *)
            TAGSTRING="$1"
            ;;

      esac

      shift

   done

   if [ "$ISGITBRANCH" == "yes" ] ; then
      TARBALLNAME="$WORKINGDIR-$GITBRANCH-$CURRENTDATE-$TAGSTRING.tar.bz2"
   else
      TARBALLNAME="$WORKINGDIR-$CURRENTDATE-$TAGSTRING.tar.bz2"
   fi

   echo "The tar-ball to be generated is '../$TARBALLNAME'..."

   if [ "$DODRYRUN" == "yes" ] ; then
      if [ "$DOCLEAN" == "yes" ] ; then
         echo "midicvt will be cleaned by 'bootstrap --clean'."
      fi
      exit 1
   fi

   if [ "$DOCLEAN" == "yes" ] ; then
      bootstrap --clean
   fi

   pushd ..
   if [ -d $WORKINGDIR ] ; then

# Others that mostly, but not always, are not needed, and so aren't excluded
# at this time:
#
# Makefile
# Makefile.in
# Linux executable files
# --exclude="aux-files"

   tar cjf $TARBALLNAME $WORKINGDIR/ \
  --exclude-vcs         \
  --exclude="Debug"     \
  --exclude="Release"   \
  --exclude="debug"     \
  --exclude="release"   \
  --exclude="autom4te.cache" \
  --exclude="core"      \
  --exclude="moc"       \
  --exclude="obj"       \
  --exclude="html"      \
  --exclude="ipch"      \
  --exclude="latex"     \
  --exclude="out.*"     \
  --exclude=".deps"     \
  --exclude=".exes"     \
  --exclude=".libs"     \
  --exclude="*.a"       \
  --exclude="*.bak"     \
  --exclude="*.BAK"     \
  --exclude="*.bpi"     \
  --exclude="*.bz2"     \
  --exclude="*.deps"    \
  --exclude="*.idb"     \
  --exclude="*.ilc"     \
  --exclude="*.ild"     \
  --exclude="*.ilf"     \
  --exclude="*.o"       \
  --exclude="*.lo"      \
  --exclude="*.la"      \
  --exclude="*.lib"     \
  --exclude="*.log"     \
  --exclude="*.obj"     \
  --exclude="*.pch"     \
  --exclude="*.pdb"     \
  --exclude="*.Po"      \
  --exclude="*.sdf"     \
  --exclude="*.so"      \
  --exclude="*.swp"     \
  --exclude=".*.swp"    \
  --exclude="*.t"       \
  --exclude="*.tds"     \
  --exclude="*.tmp"     \
  --exclude="*.user"    \
  --exclude="*.testsettings"

      echo

   else

      echo "? Working directory $WORKINGDIR does not exist above this directory."
      echo "  Are you running pack.sh from the proper directory?"
      echo "  That is what you must do.  See './pack --help'."

   fi

   popd

fi

#******************************************************************************
# pack (midicvt)
#------------------------------------------------------------------------------
# Local Variables:
# End:
#------------------------------------------------------------------------------
# vim: ts=3 sw=3 et ft=sh
#------------------------------------------------------------------------------
